package com.service.school.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dao.base.BaseDaoI;
import com.model.TClass;
import com.model.TCollege;
import com.model.TGrade;
import com.model.TMajor;
import com.pageModel.ClassInfo;
import com.pageModel.DataGrid;
import com.service.school.ClassServiceI;

@Service(value = "classService")
public class ClassServiceImpl implements ClassServiceI {

	private BaseDaoI<TClass> classDao;
	
	public BaseDaoI<TClass> getClassDao() {
		return classDao;
	}
	@Autowired
	public void setClassDao(BaseDaoI<TClass> classDao) {
		this.classDao = classDao;
	}
	
	private BaseDaoI<TGrade> gradeDao;

	public BaseDaoI<TGrade> getGradeDao() {
		return gradeDao;
	}
	@Autowired
	public void setGradeDao(BaseDaoI<TGrade> gradeDao) {
		this.gradeDao = gradeDao;
	}
	
private BaseDaoI<TMajor> majorDao;
	
	public BaseDaoI<TMajor> getMajorDao() {
		return majorDao;
	}
	@Autowired
	public void setMajorDao(BaseDaoI<TMajor> majorDao) {
		this.majorDao = majorDao;
	}

	@Override
	public List<TClass> getClassCmb(ClassInfo classInfo) {
		String hql="from TClass t where t.isEnable ='是'";
		List<TClass> l = classDao.find(hql);
		return l;
	}
	@Override
	public DataGrid datagrid(ClassInfo classInfo) {
		DataGrid dg=new DataGrid();
		String hql="%s from TClass t  %s where 1=1 ";
		if(classInfo.getClassName()!=null&&!classInfo.getClassName().trim().equals("")){
			hql+=" and t.className like'%"+classInfo.getClassName()+"%'";
		}
		if(classInfo.getClassId()!=null&&!classInfo.getClassId().trim().equals("")){
			hql+=" and t.classId like'%"+classInfo.getClassId()+"%'";
		}
		if(classInfo.getIsEnable()!=null&&!classInfo.getIsEnable().trim().equals("")){
			hql+=" and t.isEnable like'%"+classInfo.getIsEnable()+"%'";
		}
		List<TClass> l = classDao.find(String.format(hql, "", " left join fetch t.TGrade left join fetch t.TMajor"), classInfo.getPage(), classInfo.getRows());
		List<ClassInfo> nl = new ArrayList<ClassInfo>();
		if (l != null && l.size() > 0) {
			for (TClass t : l) {
				ClassInfo u = new ClassInfo();
				BeanUtils.copyProperties(t, u);
				if (t.getTGrade() != null) {
					u.setGradeName(t.getTGrade().getGradeName());
				}
				if (t.getTMajor() != null) {
					u.setMajorName(t.getTMajor().getMajorName());
				}
				nl.add(u);
			}
		}
		dg.setTotal(classDao.count(String.format(hql, "select count(*) ", "")));
		dg.setRows(nl);
		return dg;
	}
	@Override
	public void remove(String ids) {
		String[] nids = ids.split(",");
		String hql = "delete TClass t where t.id in(";
		for (int i = 0; i < nids.length; i++) {
			if (i > 0) {
				hql += ",";
			}
			hql += "'" + nids[i] + "'";
		}
		hql += ")";
		classDao.executeHql(hql);
	}
	@Override
	public boolean add(ClassInfo classInfo) {
		Long count = classDao.count("select count(*) from TClass obj where obj.classId=?", new Object[] {classInfo.getClassId()});
		if (count != null && count > 0) {
			 return  false;
		}
		TClass t=new TClass();
		t.setId(UUID.randomUUID().toString());
		t.setClassId(classInfo.getClassId());
		t.setClassName(classInfo.getClassName());
		t.setIsEnable("是");
		List<TGrade> grades = (List<TGrade>)gradeDao.find("from TGrade obj where obj.gradeName=?", new Object[]{classInfo.getGradeName()});
		if (grades != null && grades.size() == 1) {
			TGrade grade=grades.get(0);
			t.setTGrade(grade);
		}
		List<TMajor> majors = (List<TMajor>)majorDao.find("from TMajor obj where obj.majorName=?", new Object[]{classInfo.getMajorName()});
		if (majors != null && majors.size() == 1) {
			TMajor major=majors.get(0);
			t.setTMajor(major);
		}
		classDao.save(t);
		return true;
		
	}
	@Override
	public void edit(ClassInfo classInfo) {
		TClass t=classDao.get(TClass.class,classInfo.getId());
		BeanUtils.copyProperties(classInfo, t);
		List<TGrade> grades = (List<TGrade>)gradeDao.find("from TGrade obj where obj.gradeName=?", new Object[]{classInfo.getGradeName()});
		if (grades != null && grades.size() == 1) {
			TGrade grade=grades.get(0);
			t.setTGrade(grade);
		}
		List<TMajor> majors = (List<TMajor>)majorDao.find("from TMajor obj where obj.majorName=?", new Object[]{classInfo.getMajorName()});
		if (majors != null && majors.size() == 1) {
			TMajor major=majors.get(0);
			t.setTMajor(major);
		}
	}

}
